package com.budwk.app.sqfwV2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.exception.ServiceException;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfwDoc.dto.TemplateCodeUpdateDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.app.sqfwV2.dto.entity.SqfwShopsBaseInfo;
import com.budwk.app.sqfwV2.service.CenturyCitySecurityService;
import com.budwk.app.sqfwV2.service.DayingRoadService;
import com.budwk.app.sys.services.SysDictService;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiFormParams;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @author yg
 * 世纪城安全巡查表接口管理
 */

@IocBean
@At("/platform/sqfwV2/centuryCitySecurity")
@ApiDefinition(tag = "世纪城安全巡查表接口管理")
@Slf4j
public class CenturyCitySecurityController {

    @Inject(value = "centuryCitySecurityService")
    private CenturyCitySecurityService centuryCitySecurityService;

    @Inject
    private DayingRoadService dayingRoadService;
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    @Inject
    private SysDictService sysDictService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "提交数据/生成PDF")
    @ApiFormParams(implementation = TemplateCodeUpdateDTO.class)
    @ApiResponses(implementation = GenerateExcelUrlVO.class)
    @SaCheckLogin
    public Result<?> submitData(@Param("..") TemplateCodeUpdateDTO dto) {
        log.info("请求接口开始时间：" + new Date());
        log.info("CenturyCitySecurityController submitData param: {}", JSONObject.toJSONString(dto));
        return Result.data(centuryCitySecurityService.submitData(dto));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "重新生成PDF")
    @SaCheckLogin
    public Result<?> reGeneratePDF(@Param("sqfwFlowId") String sqfwFlowId) {
        log.info("CenturyCitySecurityController reGeneratePDF sqfwFlowId: {}", sqfwFlowId);
        return centuryCitySecurityService.reGeneratePDF(sqfwFlowId);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "下载Excle台账(按照编码下载)")
    @ApiResponses(implementation = GenerateExcelUrlVO.class)
    @SaCheckLogin
    public Result<?> downExcelLedger(@Param("..") DownExcelLedgerReqVo reqVo) {
        log.info("CenturyCitySecurityController downExcelLedger reqVo: {}", JSONObject.toJSONString(reqVo));
        reqVo.setAgainGenerate(true);
        return centuryCitySecurityService.downExcelLedger(reqVo);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "下载pdf(按照编码下载)")
    @ApiResponses(implementation = GenerateExcelUrlVO.class)
    @SaCheckLogin
    public Result<?> downPdfLedger(@Param("..") DownExcelLedgerReqVo reqVo) {
        log.info("CenturyCitySecurityController downExcelLedger reqVo: {}", JSONObject.toJSONString(reqVo));
        reqVo.setAgainGenerate(true);
        if (reqVo.isCheckData()) {
            List<String> indexList = centuryCitySecurityService.getCheckDataIndexList(reqVo.getSqfwIds());
            if (indexList.isEmpty()) throw new ServiceException("暂无数据，无法下载!");
            if (!indexList.contains(reqVo.getExcelCodes().get(0))) throw new ServiceException("暂无数据，无法下载!");
        }
        return centuryCitySecurityService.downPdfLedger(reqVo);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "下载文件列表数据")
    @ApiResponses(implementation = DownFileTypesResVo.class)
    @SaCheckLogin
    public Result<List<DownFileTypesResVo>> listDownFileTypes(@Param("sqfwId") String sqfwId) {
        log.info("CenturyCitySecurityController listDownFileTypes start");
        return centuryCitySecurityService.listDownFileTypes(sqfwId);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "根据输入的查询条件和内容查询列表信息")
    @ApiResponses(implementation = Sqfw_formSummary_new.class)
    @SaCheckLogin
    public Result<?> listByConditionKey(@Param("content") String content, @Param("conditionKey") String conditionKey) {
        log.info("CenturyCitySecurityController listByConditionKey content: {}, conditionKey: {}", content, conditionKey);
        return centuryCitySecurityService.listByConditionKey(conditionKey, content, SupportBusinessTypeEnum.SAFE_CHECK.getName());
        //return dayingRoadService.listByConditionKey(conditionKey, content, ServiceTypeEnum.SAFE_CHECK.getName());
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "按照商铺名称和地址去查询最新数据（精确查询）")
    @ApiResponses(implementation = Sqfw_formSummary_new.class)
    @SaCheckLogin
    public Result<?> queryByPlaceAndAddress(@Param("placeName") String placeName, @Param("address") String address) {
        log.info("CenturyCitySecurityController listByConditionKey placeName: {}, conditionKey: {}", placeName, address);
        List<Sqfw_formSummary_new> list = sqfwFormsummaryNewService.queryByPlaceAndAddress(placeName, address, SupportBusinessTypeEnum.SAFE_CHECK.getName(), null);
        return Result.success(list);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "数据检测")
    @SaCheckLogin
    public Result<?> getCheckDataIndexList(@Param("sqfwIdList") List<String> sqfwIdList) {
        log.info("centuryCity CheckDataIndexList param: {}", JSONObject.toJSONString(sqfwIdList));
        return Result.data(centuryCitySecurityService.getCheckDataIndexList(sqfwIdList));
    }


    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "根据输入的查询条件和内容查询商铺名称信息")
    @ApiResponses(implementation = SqfwShopsBaseInfo.class)
    @SaCheckLogin
    public Result<?> getShopInfoListByContent(@Param("content") String content, @Param("pageSize") Integer pageSize, @Param("pageNo") Integer pageNo) {
        log.info("CenturyCitySecurityController getShopInfoListByContent content: {},pageSize{},pageNo{}", content, pageSize, pageNo);
        return centuryCitySecurityService.getShopInfoListByContent(content, pageSize, pageNo);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "根据选择的商铺ID查询最新的记录")
    @ApiResponses(implementation = Sqfw_formSummary_new.class)
    @SaCheckLogin
    public Result<?> getFormSummaryInfoListByShopId(@Param("shopBaseInfoId") String shopBaseInfoId) {
        log.info("CenturyCitySecurityController getFormSummaryInfoListByContent shopBaseInfoId: {}", shopBaseInfoId);
        return centuryCitySecurityService.getFormSummaryInfoListByShopId(shopBaseInfoId);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "生成上报材料（检测数据）")
    @ApiResponses(implementation = DownFileTypesResVo.class)
    @SaCheckLogin
    public Result<?> getReportMaterials(@Param("fillStartDate") String fillStartDate, @Param("fillEndDate") String fillEndDate, @Param("serviceType") String serviceType, @Param("shopInfoId") String shopInfoId,@Param("placeType") String placeType) {
        log.info("CenturyCitySecurityController getReportMaterials fillStartDate: {},fillEndDate{},serviceType:{},shopInfoId:{},placeType:{}", fillStartDate, fillEndDate, serviceType, shopInfoId,placeType);
        if (StringUtils.isBlank(serviceType)) {
            serviceType = SupportBusinessTypeEnum.SAFE_CHECK.getName();
        }
        return centuryCitySecurityService.getReportMaterials(fillStartDate, fillEndDate, serviceType, shopInfoId,placeType);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "下载文件")
    @ApiResponses(implementation = GenerateExcelUrlVO.class)
    @SaCheckLogin
    public Result<?> downloadFile(@Param("..") DownExcelLedgerReqVo reqVo) {
        log.info("CenturyCitySecurityController downDataFile reqVo: {}", JSONObject.toJSONString(reqVo));
        reqVo.setSource("V2");
        if (StringUtils.isBlank(reqVo.getDownFileType())) {
            throw new ServiceException("下载文件类型不能为空！");
        }
        if (ObjectUtils.isEmpty(reqVo.getFileCode())) {
            throw new ServiceException("下载文件索引不能为空！");
        }
        List<String> fileCodes = Lists.newArrayList();
        fileCodes.add(reqVo.getFileCode());
        reqVo.setExcelCodes(fileCodes);
        //下载数据
        if ("PDF".equalsIgnoreCase(reqVo.getDownFileType())) {
            return downPdfLedger(reqVo);
        } else if ("EXCEL".equalsIgnoreCase(reqVo.getDownFileType())) {
            //兼容老数据，默认传一个值
            reqVo.setSqfwIds(List.of("0"));
            return downExcelLedger(reqVo);
        } else {
            throw new ServiceException("下载的文件类型不存在！");
        }

    }


    @At()
    @Ok("json")
    @GET
    @SLog(value = "查询数据字典（查询指定项）")
    @ApiOperation(name = "根据父级字典名称查询子级并根据location排序")
    @ApiResponses
    public Result<?> listDictChildByName(@Param("dictName") String dictName,@Param("code") String code,@Param("isCounty") boolean isCounty) {
        return Result.data(sysDictService.listDictChildByName(dictName,code,isCounty));
    }

    @At()
    @Ok("json")
    @GET
    @SLog(value = "通过code 查询字典名称")
    @ApiOperation(name = "通过code 查询字典名称")
    @ApiResponses
    public Result<?> getPlotByCode(@Param("code") String code) {
        return Result.data(sysDictService.getByCode(code));
    }

}












